package 周赛._279;

import java.util.Arrays;

/**
 * @author 会玩的洋洋
 */
public class _6002_设计位集 {
}

/**
 * 方法二 使用一个数组  加一个 flag 标记
 */
class Bitset {
    int[] bitset;
    boolean flag = false;
    int size, count;
    public Bitset(int size) {
        bitset = new int[size];
        this.size = size;
        count = 0;
    }

    public void fix(int idx) {
        if ((bitset[idx] == 0 && !flag) || (bitset[idx] == 1 && flag)) {
            bitset[idx] = 1 - bitset[idx];
            ++count;
        }
    }

    public void unfix(int idx) {
        if ((bitset[idx] == 1 && !flag) || (bitset[idx] == 0 && flag)) {
            bitset[idx] = 1 - bitset[idx];
            --count;
        }
    }

    public void flip() {
        flag = !flag;
        count = size - count;
    }

    public boolean all() {
        return size == count;
    }

    public boolean one() {
        return count > 0;
    }

    public int count() {
        return count;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i : bitset) {
            if (flag) {
                sb.append(1 - i);
            } else {
                sb.append(i);
            }
        }
        return sb.toString();
    }
}

/*
方法一  通过两个数组进行操作
class Bitset {
    int[] bitset;
    int[] temp;
    int size;
    int count;
    public Bitset(int size) {
        bitset = new int[size];
        temp = new int[size];
        Arrays.fill(temp, 1);
        this.size = size;
    }

    public void fix(int idx) {
        if (bitset[idx] == 0) {
            ++count;
            bitset[idx] = 1;
            temp[idx] = 0;
        }
    }

    public void unfix(int idx) {
        if (bitset[idx] == 1) {
            --count;
            bitset[idx] = 0;
            temp[idx] = 1;
        }
    }

    public void flip() {
        int[] temp = bitset;
        bitset = this.temp;
        this.temp = temp;
        count = size - count;
    }

    public boolean all() {
        return size == count;
    }

    public boolean one() {
        return count > 0;
    }

    public int count() {
        return count;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i : bitset) {
            sb.append(i);
        }
        return sb.toString();
    }
}
*/
